#ifndef TCP_MODB_DEV_H
#define TCP_MODB_DEV_H
#include <QThread>
#include <QTcpSocket>
#include "Comstruct.h"
#include "KgGlobalData.h"

//modbus接受数据
typedef struct _SMdobusTcpReply
{
	quint16 usEvent;       // 事务标识
	quint16 usProto;       // 协议标识
	quint16 usLen;         // 长度
 	uchar uaAddr;          // 地址
	uchar uaFuncCode;      // 功能码
	uchar usDataLen;       // 回复数据长度
	
	uchar uaSendFuncCode; //发送数据时功能码
	uchar uaSendRegAddr;  //发送数据时寄存器地址
	
}SMdobusTcpReply;
//added end


class CModbusFactory
{
  friend class TcpModbusDev;
  friend class CPCS;
public:
	CModbusFactory();
	~CModbusFactory();

public: 
	int analyReplayDatagram(char* pDataBuf, int iLen, SMdobusTcpReply& reply, int readRegCount);   	
public:
    //公共函数
	int addParam2Buffer(char* pData, quint32 uiParamVal);
	int addParam2Buffer(char* pData, quint16 uiParamVal);
	int addParam2Buffer(char* pData, float fParamVal);
	int addParam2Buffer(char* pData, uchar ucParamVal);
	int addParam2BufferSmall(char* pData, quint16 uiParamVal);
	int getParamFromBuffer(char* pData, uchar& uiParamVal);
	int getParamFromBuffer(char* pData, qint16& uiParamVal);
	int getParamFromBuffer(char* pData, quint16& uiParamVal);
	int getParamFromBuffer(char* pData, quint32& uiParamVal);
    int getParamFromBuffer(char* pData, quint64& uiParamVal);
	int getParamFromBuffer(char* pData, float& fParamVal);
	int getParamFromBufferSmall(char* pData, float& fParamVal); 
	int formDatagram(quint16 usEvent, uchar uaAddr, uchar uaFuncCode, quint16 usStartReg, quint16 usRegNum, char* pSendDataBuf);
    int formSetSinglegram(quint16 usEvent, uchar uaAddr, quint16 usRegAddr, quint16 usRegVal, char* pSendDataBuf);
    int formSetMultgram(quint16 usEvent, uchar uaAddr, quint16 usRegAddr, quint16 regCount, quint16 *usRegValBuf, char* pSendDataBuf);//设置多个保持寄存器报文 added by zyx 2017-3-23 

	int  analySetSingleDatagram(quint16 usSdEvent, char* pRecDataBuf, int iLen, quint16 usRegAddr, quint16 usRegVal, char devAddr);
	int  analySetMultDatagram(quint16 usSdEvent, char* pRecDataBuf, int iLen, quint16 usRegAddr, quint16 regCount);
	
private: 
};

typedef struct
{
	//int xfSpray; // 消防喷洒
	//int gasAlarm;// 可燃气体
	int xfState;
	int otherState;
    int aZlThunder;  // 直流防雷
    int aJlThunder;  // 交流防雷
    int aDoorAlarm;  // 集装箱门禁
    int aFireWarn;   // 消防告警
    int aFireAction; // 消防动作
    int aFireFault;  // 消防故障
    int aJlDropElec; // 交流失电
    int aUpsError;   // UPS故障

	int devAddr;
	int connError;
	int failedCount;
	int bReaded;
}BrotherBaData;

//读取modbus设备数据 added 2023-4-25
class TcpModbDev : public QThread
{
	Q_OBJECT

public:
	TcpModbDev(QObject *parent=NULL);
	~TcpModbDev();
   void setPara(QString strIp, int port);
public:
   void run();
protected:
   void KgSleep(int timespan);
   int  sendRecvData(char* pataBuf, int readRegCount, int iRetVal);
   void readReplyHead(char* pDataBuf, SMdobusTcpReply &reply, int &iInedx);
   void JudgeDevConnError(BrotherBaData &dev);

   void KgReadDevData(BrotherBaData &dev);
   void readDevData1(char* pRevDataBuf, int iRetVal, BrotherBaData &dev);
   // 从机BA写入单体最高最低温度数据给主机BA (usStackHoldTemp[0] to usStackHoldTemp[3])
   void writeMinMaxTempData(); // added 2023-11-28
   void writeSlaveData();// 从机写入数据给主机 // added 2023-12-29
 
   void SaveSendRecvLog( unsigned char *data, int dataLength, bool isSended);
private slots:
   void disconnected();
signals:
	void addLog(int iLogType, const QString& strLog); 
	
private:
	char *m_pDataBuf;
	char *m_pSendBuf;
	uchar m_uaFuncCode;
	int   m_usRegAddr;

	QString     m_strIP;       // Dev IP
	ushort      m_usPort;      // Dev Port
	int         m_saveRdCount;
	
	QTcpSocket* m_pTcpClient;
	bool        m_bTcpConnected;
	int         m_iErrNum;
	
	CModbusFactory* m_pModbusFactory; 
	int         m_connErrorLog;
};
extern BrotherBaData g_brotherBaData;
extern QString g_brotherBaIp;  //  另一台BA的IP
extern int g_brotherBaPort;    //  另一台BA的端口
#endif // TCP_MODB_DEV_H
